package com.zeedev.flyingsaucer.util;

import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FrozenExpandableListBehavior implements OnGroupCollapseListener {

	//needs target
//	Class<? extends ExpandableListView> elv = null;
	Object target = null;
	
	public FrozenExpandableListBehavior(
			Object target2) {
		target = target2;
	}

	@Override
	public void onGroupCollapse(int groupPosition) {
		//super.onGroupCollapse(groupPosition);
		ExpandableListView.class.cast(target).expandGroup(groupPosition);
//		elv.expandGroup(groupPosition);
		
	}
	
	public static void bind(Object target) {
		try {
			safebind(target);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void safebind(Object target) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		
		//this.getExpandableListView().setOnGroupCollapseListener(new FrozenExpandableListBehavior());
		
		Method m = target.getClass().getMethod("getExpandableListView", (Class[])null);
		Object expListView = m.invoke(target, null);
		Method m2 = expListView.getClass().getMethod("setOnGroupCollapseListener", OnGroupCollapseListener.class );
		
		FrozenExpandableListBehavior o = new FrozenExpandableListBehavior(expListView);
		m2.invoke(expListView, o);
		
		
//		return null;
		
	}

	
	
	
	
}
